package com.rw.test.config.exception;

import com.rw.engine.frequency.exception.FrequencyLimitException;
import com.rw.test.vo.ResultVO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class FrequencyExceptionHandler {
    @ResponseBody
    @ExceptionHandler(FrequencyLimitException.class)
    public ResultVO<?> frequencyLimitExceptionHandler(FrequencyLimitException exception) {
        switch (exception.getExceptionEnum()) {
            case TOO_CLOSED:
                return new ResultVO<>(1, "两次访问时间间隔太短！");
            case TOO_MANY_REQUESTS:
                return new ResultVO<>(1, "规定时间内访问次数太多！");
            default:
                throw new IllegalArgumentException(exception);
        }
    }
}
